<?php
class MembersController extends AppController {
	var $name = 'Members';
    var $rank = array(
        '1'  => 'Member',
        '10' => 'Distributor',
        '20' => 'Ruby',
        '30' => 'Sapphire',
        '40' => 'Diamond',
        '50' => 'Royal Golden Diamond'
    );

    public function admin_listData ($type=null) {
        $rs = $this->Member->accountList($this->params['form']);

        $json = ($rs) ? $rs : array(
            'page'  => 1,
            'total' => 0,
            'rows'  => array()
        );

        $this->response($json);
    } // admin_listData

    public function admin_dashboard () {
        //$this->stylesheet  = am($this->stylesheet, 'flexigrid/flexigrid', 'jscal2/jscal2', 'jscal2/border-radius', 'popup');
        //$this->javascripts = am($this->javascripts, 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'flexigrid.min.js', 'jscal2.js', 'admin_member');

        //$this->loadModel('Membership');

        $this->pageTitle = __('Dashboard', true);
        $this->set('pageTitle', $this->pageTitle);
        //$this->set('data', $this->Member->find('first', array('fields'=>'id, member_id, sponsor_id, member_code, rank_id, join_date, renew_date, fullname, status, tree_level')));
        //$this->set('data2', $this->Membership->find());
    } // admin_dashboard

    public function admin_genealogy () {
        if ($this->RequestHandler->isAjax()) {
            $this->loadModel('WeeklyBonus');
            $this->loadModel('Membership');

            $form = $this->params['form'];
            $id   = str_replace('node', '', $form['id']);
            $data = isset($form['init']) ? $this->Member->nodeInfo($id) : array('id'=>$form['id']);
            $node = $this->Member->findGenealogyNode($id, $form['levelsToShow']);

            $data['children'] = $node;

            if (array($data) && count($data) > 0) {
                if (count($this->Member->ids) > 0) {
                    if ($bv = $this->WeeklyBonus->getCurrentBvInfo($this->Member->ids)) {
                        $data = $this->_joinMemberData($data, $bv);
                    }
                    if ($bv = $this->Membership->getCurrentBvInfo($this->Member->ids)) {
                        $data = $this->_joinMemberData($data, $bv);
                    }
                }

                $json = array(
                    'status' => true,
                    'data'   => $data
                );
            } else {
                $json = array(
                    'status' => false
                );
            }

            $this->response($json);
        } else {
            $this->stylesheet  = am($this->stylesheet, 'jit/base');
            $this->stylesheet  = am($this->stylesheet, 'jit/Spacetree');
            $this->javascripts = am($this->javascripts, 'jit-yc', 'admin_genealogy');
            $this->pageTitle   = __('Genealogy Tree', true);

            $this->set('pageTitle', $this->pageTitle);
        }
    } // admin_genealogy

    public function admin_ajax_login () {  // AJAX request without logged, response login needed -2
        $json = array(
            'status' => -2
        );
        $this->response($json);
    } // admin_ajax_login

    public function admin_login () {
        if (isset($this->params['form']['password'])) { // check if login password correct
            if ($this->params['form']['password'] == 'abc123') {
                $json = array('status' => true);
                $this->Session->write('Admin.logged', true);
            } else {
                $json = array(
                    'status' => false,
                    'mesg'   => __('Invalid login, please try again..', true)
                );
            }

            $this->response($json);
            return;
        } else if ($this->Session->read('Admin.logged')) { // already logged mah..
            $this->redirect('/admin');
        }

        $this->pageTitle = __('Back Office', true);
        $this->set('pageTitle', $this->pageTitle);
    } // admin_login

    public function admin_logout () {
        //$logged = $this->Session->read('Admin.logged');
        $bool = $this->Session->del('Admin');
        $this->redirect('/admin/members/login');
    } // admin_logout

    public function admin_check_ranking ($member_code) {
        if (!$member_code) {
            $json = array(
                'status' => false,
                'mesg'   => __('User Id does not found', true)
            );
            $this->response($json);
            return;
        }

        $member_id = $this->Member->field('id', array('international_id = ' => $member_code));
        if ($member_id == false) {
            $json = array(
                'status' => false
            );
            $this->response($json);
            return;
        }

        $this->loadModel('Sale');
        $rank = $this->Sale->userRank($member_id);
        $rate = (!$rank || $rank == 1) ? 1.15 : 1; // normal member pay extra 15%

        $json = array(
            'status'    => true,
            'priceRate' => $rate
        );
        $this->response($json);
    } // admin_check_ranking

    public function admin_tree ($id='') {
        if ($this->RequestHandler->isAjax()) {
            $data = $this->Member->findDownlines($id);
            $this->set('data', $data);
            $this->render('tree', 'blank');
        } else {
            $this->stylesheet  = am($this->stylesheet, 'simple.tree/simple.tree');
            $this->javascripts = am($this->javascripts, 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'simple.tree');
            $this->pageTitle   = __('Members Tree', true);

            $this->set('pageTitle', $this->pageTitle);
        }
    } // admin_tree

    public function admin_update_status () {
        $json = array('status'=>1);
        $form = $this->params['form'];

        if ($form['task'] == 'delete') {
            $status = 'R';
            $action = 'delete';
        } else {
            $json = array(
                'status' => false,
                'error'  => 'Process failure, missing required data.'
            );
            $this->response($json);
            return;
        }

        if (empty($form['id'])) {
            $json = array(
                'status' => false,
                'error'  => 'Couldn\'t '.$action.' account, missing required record Id.'
            );
            $this->response($json);
            return;
        }

        $ids = explode(',', $form['id']);

        if ($form['task'] == 'delete') { // delete member record need to log the time
            $rs = $this->Member->updateAll(
                array(
                    'Member.status'      => '"'.$status.'"',
                    'Member.delete_time' => '"'.date('Y-m-d H:i:s').'"'
                ),
                array('Member.id' => $ids)
            );
        } else {
            $rs = $this->Member->updateAll(
                array('Member.status' => '"'.$status.'"'),
                array('Member.id' => $ids)
            );
        }

        if ($rs) {
            $json = array('status' => true);
        } else {
            $json = array(
                'status' => false,
                'error'  => 'Fail to '.$action.' account at this time.'
            );
        }

        $this->response($json);
    } // admin_update_status

    public function admin_update () {
        $this->data['Member'] = $this->params['form'];

        if (empty($this->data['Member']['id'])) { // add new record
            $rs = $this->Member->find('first', array(
                'recursive' => -1,
                'fields'    => 'international_id',
                'order'     => 'international_id DESC'
            ));
            if (isset($rs['Member']['international_id'])) {
                $this->data['Member']['international_id'] = $rs['Member']['international_id'] + 1;
            }
        }
        if ($this->data['Member']['member_id'] && $this->data['Member']['upline_leg'] && $this->data['Member']['reserved_id']) { // FOR TEMPORARY
            $sql = 'SELECT au.`id` FROM `members` as u INNER JOIN `members` as au ON u.`id`=au.`member_id` ' .
                   'WHERE u.`international_id`='.$this->data['Member']['member_id'].' AND au.`placement`="'.$this->data['Member']['upline_leg'].'"';
            $rs  = $this->Member->query($sql);

            if (count($rs) > 0) {
                $uid = isset($rs[0]['au']['id']) ? $rs[0]['au']['id'] : false;

                if ($uid) {
                    $sql = 'UPDATE `members` SET `international_id`="'.$this->data['Member']['reserved_id'].'" WHERE `id`='.$uid;
                    $this->Member->query($sql);
                    $this->data['Member']['member_id'] = $uid;
                }
            } else {
                $json = array(
                    'status' => false,
                    'mesg'   => __('You have entered an invalid upline id: '.$this->data['Member']['member_id'].'. Please try again.', true)
                );
                $json['error'][0] = array('id'=>'member_id', 'field'=>'member_id', 'code'=>'Upline not found.');

                $this->response($json);
                return;
            }
        } else if ($this->data['Member']['member_id']) { // get upline member id
            $rs = $this->Member->find('first', array(
                'conditions' => array('international_id' => $this->data['Member']['member_id']),
                'recursive'  => -1,
                'fields'     => 'id'
            ));
            if (isset($rs['Member']['id'])) {
                $cond = array(
                    'member_id' => $rs['Member']['id'],
                    'placement' => $this->data['Member']['placement']
                );

                if ($this->data['Member']['id']) { // use for UPDATE query
                    $cond['id <>'] = $this->data['Member']['id'];
                }

                $occupy = $this->Member->find('count', array(
                    'conditions' => $cond
                ));

                if ($occupy) { // invalid placement
                    $leg  = ($this->data['Member']['placement'] == 'A') ? 'left' : 'right';
                    $json = array(
                        'status' => false,
                        'mesg'   => __('Invalid placement to current selected Upline!<br />'.$this->data['Member']['member_id'].' has already downline under its '.$leg.' leg.', true)
                    );
                    $json['error'][0] = array('id'=>'member_id', 'field'=>'member_id', 'code'=>'please select other upline');

                    $this->response($json);
                    return;
                }

                $this->data['Member']['member_id'] = $rs['Member']['id'];
            } else {
                $json = array(
                    'status' => false,
                    'mesg'   => __('You have entered an invalid upline id: '.$this->data['Member']['member_id'].'. Please try again.', true)
                );
                $json['error'][0] = array('id'=>'member_id', 'field'=>'member_id', 'code'=>'Upline not found.');

                $this->response($json);
                return;
            }
        }
        if ($this->data['Member']['sponsor_id']) { // get sponsor id
            $rs = $this->Member->find('first', array(
                'conditions' => array('international_id' => $this->data['Member']['sponsor_id']),
                'recursive'  => -1,
                'fields'     => 'id'
            ));
            if (isset($rs['Member']['id'])) {
                $this->data['Member']['sponsor_id'] = $rs['Member']['id'];
            } else {
                $json = array(
                    'status' => false,
                    'mesg'   => __('You have entered an invalid sponsor id: '.$this->data['Member']['sponsor_id'].'. Please try again.', true)
                );
                $json['error'][0] = array('id'=>'sponsor_id', 'field'=>'sponsor_id', 'code'=>'Sponsor not found.');

                $this->response($json);
                return;
            }
        }

        if ($this->data) {
            $valid = true;

            foreach ($this->data['Member'] as $fieldname=>$value) {
                if (empty($value) && $value != '0') {
                    $this->data['Member'][$fieldname] = null;
                }
            }

            $join  = $this->data['Member']['join_date'].' '.$this->data['Member']['join_time'];
            $renew = $this->data['Member']['renew_date'].' '.$this->data['Member']['renew_time'];

            $this->data['Member']['join_date']    = $this->clientToServerTime($join);
            $this->data['Member']['renew_date']   = $this->clientToServerTime($renew);
            $this->data['Member']['default_lang'] = empty($this->data['Member']['default_lang']) ? 'en' : $this->data['Member']['default_lang'];

            $this->Member->set($this->data);

            if ($this->Member->validates() && $valid) { // data validated!
                if ($this->Member->save($this->data)) {
                    $this->loadModel('Membership');

                    $data = array(
                        'Membership' => array(
                            'MembershipID' => $this->Member->id,
                            'UplineID'     => $this->data['Member']['member_id'],
                            'SponsorID'    => $this->data['Member']['sponsor_id'],
                            'RankLookupID' => $this->data['Member']['rank_id'],
                            'DateJoin'     => $this->data['Member']['join_date'],
                            'RenewDate'    => $this->data['Member']['renew_date'],
                            'GiveName'     => $this->data['Member']['fullname'],
                            'Status'       => $this->data['Member']['status'],
                            'Country'      => $this->data['Member']['country']
                        )
                    );
                    if (isset($this->data['Member']['international_id'])) {
                        $data['Membership']['MemberCode'] = $this->data['Member']['international_id'];
                    }
                    $this->Membership->save($data);

                    if ($this->data['Member']['member_id']) { // update upline information //
                        $field = ($this->data['Member']['placement'] == 'A') ? 'leftid' : 'rightid';
                        $this->Membership->id = $this->data['Member']['member_id'];
                        $this->Membership->saveField($field, $this->Member->id);
                    }

                    $mesg = empty($this->params['form']['id']) ? __('New account has been created successfully!',true) : __('Account has been updated successfully!',true);
                    $json = array(
                        'status' => true,
                        'mesg'   => $mesg
                    );
                } else {
                    $mesg = empty($this->params['form']['id']) ? __('Fail insert record to database at this time, please try again.',true) : __('Fail update record to database at this time, please try again.',true);
                    $err  = ($this->error) ? $this->error : $mesg;
                    $json = array(
                        'status' => false,
                        'mesg'   => $err
                    );
                }
            } else { // invalid data found
                $json = array(
                    'status' => false,
                    'mesg'   => __('Please enter the requested details in the fields pointed out below and try again!', true)
                );
                foreach ($this->Member->invalidFields() as $k=>$v) {
                    $json['error'][] = array('id'=>$k, 'field'=>$k, 'code'=>$v);
                }
            }
        } else {
            $json = array(
                'status' => false,
                'mesg'   => __('no data posted', true)
            );
        }

        $this->response($json);
    } // admin_update

    public function admin_get ($id) {
        $this->data = $this->Member->findById($id);

        if (empty($this->data['Member'])) {
            $json = array(
                'status' => false,
                'mesg'   => __('No data found from database', true)
            );
        }

        $data = array();
        $i    = 0;
        foreach ($this->data['Member'] as $f=>$v) {
            $data[$i] = array(
                'f' => $f,
                'v' => $v
            );

            if ($f == 'member_id' && $v) {
                $this->Member->id = $v;
                $data[$i] = array(
                    'f' => 'member_id',
                    'v' => $this->Member->field('international_id')
                );
            } else if ($f == 'sponsor_id' && $v) {
                $this->Member->id = $v;
                $data[$i] = array(
                    'f' => 'sponsor_id',
                    'v' => $this->Member->field('international_id')
                );
            } else if ($f == 'join_date' && $v) {
                $data[$i] = array(
                    'f' => 'join_date',
                    'v' => $this->serverToClientTime($v, 'Y-m-d')
                );
                $i++;
                $data[$i] = array(
                    'f' => 'join-date',
                    'v' => $this->_dateFormat($v)
                );
                $i++;
                $data[$i] = array(
                    'f' => 'join_time',
                    'v' => $this->serverToClientTime($v, 'g:i A')
                );
            } else if ($f == 'renew_date' && $v) {
                $data[$i] = array(
                    'f' => 'renew_date',
                    'v' => ($v) ? $this->serverToClientTime($v, 'Y-m-d') : ''
                );
                $i++;
                $data[$i] = array(
                    'f' => 'renew-date',
                    'v' => ($v) ? $this->_dateFormat($v) : 'select..'
                );
                $i++;
                $data[$i] = array(
                    'f' => 'renew_time',
                    'v' => $this->serverToClientTime($v, 'g:i A')
                );
            } else if ($f == 'dob' && $v) {
                $i++;
                $data[$i] = array(
                    'f' => 'dob-date',
                    'v' => $this->_dateFormat($v)
                );
            }

            $i++;
        }

        $json = array(
            'status' => true,
            'data'   => $data
        );

        $this->response($json);
    } // admin_get

    public function admin_search () {
        $q = $this->params['url']['q'];
        $l = $this->params['url']['limit'];

        if (!$q) {
            exit;
        }

        $data = $this->Member->find('all', array(
            'conditions' => array(
                'OR' => array(
                    'international_id LIKE' => $q.'%',
                    'member_code LIKE' => $q.'%',
                    'fullname LIKE'    => '%'.$q.'%'
                )
            ),
            'recursive'  => -1,
            'fields'     => 'id, international_id, member_code, fullname',
            'order'      => 'member_code ASC',
            'limit'      => $l
        ));

        foreach ($data as $member) {
            echo $member['Member']['id'].'|'.$member['Member']['international_id'].'|'.$member['Member']['fullname'].'|'.$member['Member']['member_code']."\n";
        }

        exit;
    } // admin_search

    public function admin_print ($id) {
        $this->data = $this->Member->findById($id);

        if (empty($this->data['Member'])) {
            $message = __('No data found from database.', true);
            $this->Session->setFlash('<h4>'.$message.'</h4>', 'blank', array('class'=>'boxError'));
        }

        $data = array();

        foreach ($this->data['Member'] as $f=>$v) {
            $data[$f] = $v;

            if (($f == 'member_id' || $f == 'sponsor_id') && $v) {
                $m = $this->Member->read(array('international_id','fullname'), $v);

                $data[$f] = $m['Member']['international_id'].'/'.$m['Member']['fullname'];
            } else if ($f == 'rank_id') {
                $data[$f] = $this->rank[$v];
            } else if (($f == 'join_date' || $f == 'renew_date') && $v) {
                $data[$f] = $this->serverToClientTime($v, 'd/m/Y g:i A');
            } else if ($f == 'dob' && $v) {
                $data[$f] = $this->_dateFormat($v);
            } else if ($f == 'placement' && $v) {
                $data[$f] = ($v == 'A') ? __('A (Left Side)',true) : __('B (Right Side)',true);
            }
        }

        $this->pageTitle = __('Member Details', true);
        $this->set('data', $data);
        $this->render('admin_print', 'print');
    } // admin_print

    public function admin_index () {
        $this->loadModel('Country');
        $data = $this->Country->find('list');

        $this->stylesheet  = am($this->stylesheet, 'flexigrid/flexigrid', 'autocomplete', 'jscal2/jscal2', 'jscal2/border-radius', 'clockpick', 'popup');
        $this->javascripts = am($this->javascripts, 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'autocomplete.min', 'flexigrid.min', 'jscal2', 'clockpick.min', 'admin_member');
        $this->pageTitle   = __('Members Management', true);

        $this->set('pageTitle', $this->pageTitle);
        $this->set('ranks', $this->rank);
        $this->set('countries', $data);
        $this->render('admin_manage');
    } // admin_index

    public function login () {
        if (isset($this->params['form']['user_id']) /*&& isset($this->params['form']['password'])*/) {
            $data = $this->Member->find('first', array(
                'conditions' => array(
                    'OR' => array(
                        'international_id' => $this->params['form']['user_id'],
                        'member_code'      => $this->params['form']['user_id']
                    )//,
                    //'AND' => array('password' => $this->params['form']['password'])
                ),
                'recursive'  => -1,
                'fields'     => 'id, international_id, login_times, fullname'
            ));

            if (isset($data['Member']['id'])) {
                $this->Member->id = $data['Member']['id'];
                $this->Member->saveField('last_login_time', date('Y-m-d H:i:s'));
                $this->Member->saveField('login_times', $data['Member']['login_times']+1);

                $json = array('status' => true);
                $this->Session->write('Member', array(
                    'id'     => $data['Member']['id'],
                    'code'   => $data['Member']['international_id'],
                    'name'   => $data['Member']['fullname'],
                    'logged' => true
                ));
            } else {
                $json = array(
                    'status' => false,
                    'mesg'   => __('Invalid login, please try again..', true)
                );
            }

            $this->response($json);
            return;
        } else if ($this->Session->read('Member.logged')) { // already logged mah..
            $this->redirect('/members');
        }

        $this->pageTitle = __('Member Login', true);
        $this->set('pageTitle', $this->pageTitle);
    } // login

    public function logout () {
        $bool = $this->Session->del('Member');
        $this->redirect('/members/login');
    } // logout

    public function genealogy () {
        if (!$this->_checkLogged()) {
            return;
        }

        if ($this->RequestHandler->isAjax()) {
            $this->loadModel('WeeklyBonus');
            $this->loadModel('Membership');

            $form = $this->params['form'];
            $id   = str_replace('node', '', $form['id']);
            $data = isset($form['init']) ? $this->Member->nodeInfo($this->Session->read('Member.id')) : array('id'=>$form['id']);
            $node = $this->Member->findGenealogyNode($id, $form['levelsToShow']);

            $data['children'] = $node;

            if (array($data) && count($data) > 0) {
                if (count($this->Member->ids) > 0) {
                    if ($bv = $this->WeeklyBonus->getCurrentBvInfo($this->Member->ids)) {
                        $data = $this->_joinMemberData($data, $bv);
                    }
                    if ($bv = $this->Membership->getCurrentBvInfo($this->Member->ids)) {
                        $data = $this->_joinMemberData($data, $bv);
                    }
                }

                $json = array(
                    'status' => true,
                    'data'   => $data
                );
            } else {
                $json = array(
                    'status' => false
                );
            }

            $this->response($json);
        } else {
            $this->stylesheet  = am($this->stylesheet, 'jit/base');
            $this->stylesheet  = am($this->stylesheet, 'jit/Spacetree');
            $this->javascripts = am($this->javascripts, 'jit-yc', 'admin_genealogy');
            $this->pageTitle   = __('Genealogy Tree', true);

            $this->set('pageTitle', $this->pageTitle);
        }
    } // genealogy

    public function indented ($ax='') {
        if (!$this->_checkLogged()) {
            return;
        }

        if ($ax || $this->RequestHandler->isAjax()) {
            $id   = $this->Session->read('Member.id');
            $data = $this->Member->indented($id);

            $this->loadModel('WeeklyBonus');
            $data = $this->WeeklyBonus->bvInfo($data);

            $this->set('data', $data);
            $this->render('indented_tbl', 'blank');
        } else {
            $left  = $this->Member->totalDownlines($this->Session->read('Member.id'), 'A');
            $right = $this->Member->totalDownlines($this->Session->read('Member.id'), 'B');

            $this->javascripts = am($this->javascripts, 'members/indented');
            $this->pageTitle   = __('Indented List Mode', true);

            $this->set('pageTitle', $this->pageTitle);
            $this->set('left', $left);
            $this->set('right', $right);
        }
    } // indented

    public function downlines () {
        if (!$this->_checkLogged()) {
            return;
        }

        if ($this->RequestHandler->isAjax()) {
            $id   = $this->Session->read('Member.id');
            $rs   = $this->Member->downlines($this->params['form'], $id);
            $json = ($rs) ? $rs : array(
                'page'  => 1,
                'total' => 0,
                'rows'  => array()
            );

            $this->response($json);
        } else {
            $this->stylesheet  = am($this->stylesheet, 'flexigrid/flexigrid');
            $this->javascripts = am($this->javascripts, 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'flexigrid.min', 'members/downlines');
            $this->pageTitle   = $this->Session->read('Member.name').' '.__('Downline Listings', true);

            $this->set('pageTitle', $this->pageTitle);
        }
    } // downlines

    public function sponsored ($ax='') {
        if (!$this->_checkLogged()) {
            return;
        }

        if ($ax || $this->RequestHandler->isAjax()) {
            $id   = $this->Session->read('Member.id');
            $data = $this->Member->sponsored($id);

            $this->loadModel('WeeklyBonus');
            $data = $this->WeeklyBonus->bvStatus($data);

            $this->set('data', $data);
            $this->render('sponsored_tbl', 'blank');
        } else {
            $this->javascripts = am($this->javascripts, 'members/sponsored');
            $this->pageTitle   = __('List of Sponsored Members', true);

            $this->set('pageTitle', $this->pageTitle);
        }
    } // sponsored

    public function update () {
        if (!$this->_checkLogged()) {
            return;
        }

        if ($this->RequestHandler->isAjax()) {
            $this->Member->read(null, $this->params['form']['id']);
            $this->Member->set($this->params['form']);

            if ($this->Member->validates()) { // data validated!
                if ($this->Member->save()) {
                    $mesg = __('Your account has been updated successfully!',true);
                    $json = array(
                        'status' => true,
                        'mesg'   => $mesg
                    );
                } else {
                    $mesg = __('Fail update your record to database at this time, please try again.',true);
                    $err  = ($this->error) ? $this->error : $mesg;
                    $json = array(
                        'status' => false,
                        'mesg'   => $err
                    );
                }
            } else { // invalid data found
                $json = array(
                    'status' => false,
                    'mesg'   => __('Please enter the requested details in the fields pointed out below and try again!', true)
                );
                foreach ($this->Member->invalidFields() as $k=>$v) {
                    $json['error'][] = array('id'=>$k, 'field'=>$k, 'code'=>$v);
                }
            }

            $this->response($json);
            return;
        } else {
            $data = $this->Member->find('first', array(
                'conditions' => array(
                    'id' => $this->Session->read('Member.id')
                ),
                'recursive'  => -1
            ));

            $day = $this->serverToClientTime($data['Member']['dob'], 'D');
            $data['Member']['dob-date'] = substr($day, 0, 2).$this->serverToClientTime($data['Member']['dob'], 'j F Y');

            $this->stylesheet  = am($this->stylesheet, 'jscal2/jscal2', 'jscal2/border-radius');
            $this->javascripts = am($this->javascripts, 'jscal2', 'members/member_update');
            $this->pageTitle   = __('Member Information Modify', true);
            $this->set('pageTitle', $this->pageTitle);
            $this->set('member', $data['Member']);
        }
    } // update

    public function password () {
        if (!$this->_checkLogged()) {
            return;
        }

        if ($this->RequestHandler->isAjax()) {
            if (!empty($this->params['form']['old_password']) || !empty($this->params['form']['password'])) {
                $data = $this->Member->find('count', array(
                    'conditions' => array(
                        'id'       => $this->Session->read('Member.id'),
                        'password' => $this->params['form']['old_password']
                    ),
                    'recursive'  => -1
                ));

                if (!$data) {
                    $json = array(
                        'status' => false,
                        'mesg'   => __('You have enter an incorrect old password!', true)
                    );
                    $json['error'][] = array('id'=>'old_password', 'field'=>'old_password', 'code'=>__('incorect password',true));
                    $this->response($json);
                    return;
                }

                $this->Member->id = $this->Session->read('Member.id');
                $this->Member->saveField('password', $this->params['form']['password']);
                $mesg = __('Your password has been changed successfully!',true);
                $json = array(
                    'status' => true,
                    'mesg'   => $mesg
                );
            } else {
                $json = array(
                    'status' => false,
                    'mesg'   => __('Please enter old and new password!', true)
                );
            }

            $this->response($json);
            return;
        } else {
            $this->javascripts = am($this->javascripts, 'jscal2', 'members/member_password');
            $this->pageTitle   = __('Member Password Modify', true);
            $this->set('pageTitle', $this->pageTitle);
        }
    } // password

    public function index () {
        $this->_checkLogged();

        $this->pageTitle = __('ATP Member Area', true);
        $this->set('pageTitle', $this->pageTitle);
    } // index


    private function _dateFormat ($date) {
        $time = strtotime($date);
        $day  = date('D', $time + CLIENT_TIMEZONE_OFFSET);
        $date = date(', j F Y', $time + CLIENT_TIMEZONE_OFFSET);
        $date = substr($day, 0, 2).$date;
        return $date;
    } // _dateFormat

    private function _joinMemberData ($data1, $data2) {
        $id = str_replace('node', '', $data1['id']);

        if (array_key_exists($id, $data2)) {
            $data1['data'] = array_merge($data1['data'], $data2[$id]);
        }

        if (is_array($data1['children']) && count($data1['children']) > 0) {
            foreach ($data1['children'] as $i=>$child) {
                $children[$i] = $this->_joinMemberData($child, $data2);
            }
            $data1['children'] = $children;
        }

        return $data1;
    } // _joinMemberData
} // MembersController
?>